BIN_PKG_NAME 	:= binutils
PKG_VER 	:= $(call qstrip,$(CONFIG_BINUTILS_VER))
BIN_PKG_SRC	:= $(BIN_PKG_NAME)-$(PKG_VER).tar.bz2
BIN_PKG_SITE	:= $(MK_GNU_MIRROR)/$(BIN_PKG_NAME)
PKG_DIR		:= $(TOOL_BUILD_DIR)/$(BIN_PKG_NAME)-$(PKG_VER)
PKG_BUILD_DIR	:= $(TOOL_BUILD_DIR)/$(BIN_PKG_NAME)-$(PKG_VER)-build

EXTRA_BINUTILS_CONFIG_OPTIONS := ""
# We do not rely on the host's gmp/mpfr but use a known working one
#
# not support gmp / prfr lib now
BINUTILS_HOST_PREREQ:=
BINUTILS_TARGET_PREREQ:=

$(STAGING_DIR)/usr/bin/$(REAL_GNU_TARGET_NAME)-ld: $(PKG_BUILD_DIR)/binutils/objdump
	$(MAKE) -C $(PKG_BUILD_DIR) $(TCHAIN_SYSROOT_STAGING_DESTDIR) install


$(PKG_DIR)/.unpacked: binutils_source
	rm -rf $(PKG_DIR)
	[ -d $(TOOL_BUILD_DIR) ] || $(INSTALL) -d $(TOOL_BUILD_DIR)
	[ -d $(PKG_DIR) ] || \
		tar -C $(TOOL_BUILD_DIR) $(TAR_OPTIONS) $(DL_DIR)/$(BIN_PKG_SRC)
	touch $@

$(PKG_DIR)/.patched: $(PKG_DIR)/.unpacked
	# Apply appropriate binutils patches.
	#
	scripts/qpatch.sh $(PKG_DIR) $(TCHAIN_PATCH_DIR)/$(BIN_PKG_NAME)/$(PKG_VER)
	touch $@


	#	--host=$(GNU_HOST_NAME) \
		#
$(PKG_BUILD_DIR)/.configured: $(PKG_DIR)/.patched
	mkdir -p $(PKG_BUILD_DIR)
	(cd $(PKG_BUILD_DIR); rm -rf config.cache; \
		$(HOST_CONFIGURE_OPTS) \
		$(PKG_DIR)/configure \
		--prefix=$(TCHAIN_SYSROOT_PREFIX)/usr \
		--build=$(GNU_HOST_NAME) \
		--target=$(REAL_GNU_TARGET_NAME) \
		$(TCHAIN_CONFIG_DEVEL_SYSROOT) \
		$(TCHAIN_CONFIG_STAGING_SYSROOT) \
		$(DISABLE_NLS) \
		$(MULTILIB) \
		--disable-werror \
		$(SOFT_FLOAT_CONFIG_OPTION) \
		$(EXTRA_BINUTILS_CONFIG_OPTIONS) \
		$(QUIET) \
	)
	touch $@

$(PKG_BUILD_DIR)/binutils/objdump: $(PKG_BUILD_DIR)/.configured
	$(MAKE) -C $(PKG_BUILD_DIR) all

binutils: $(BINUTILS_HOST_PREREQ) $(STAGING_DIR)/usr/bin/$(REAL_GNU_TARGET_NAME)-ld

#
#	$(CONFIG_UPDATE) $(@D)
#

binutils_target: $(BINUTILS_TARGET_PREREQ) $(TARGET_DIR)/usr/bin/ld

binutils_target-clean:
	-$(MAKE) -C $(BINUTILS_DIR2) clean
	rm -f $(TARGET_DIR)/bin/$(REAL_GNU_TARGET_NAME)* \
		$(addprefix $(TARGET_DIR)/usr/bin/, addr2line ar as gprof ld nm objcopy objdump ranlib readelf size strings strip c++filt)

binutils_target-dirclean:
	rm -rf $(BINUTILS_DIR2)

binutils_source:
	$(call DOWNLOAD,$(BIN_PKG_SITE),$(BIN_PKG_SRC))
	@echo $@
